/**
 * 
 */
package edu.gtcfla.km.platform.appmodules.web;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.AbstractRefreshableConfigApplicationContext;
import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.common.configration.PreferencesRegister;
import org.vsg.common.web.json.SubmitResult;
import org.vsg.common.web.json.processor.JsDateViewJsonValueProcessor;

/**
 * @author Bill Vison
 *
 */
@Controller
@Lazy
@RequestMapping("/config")
public class ConfigController {

	private final static Logger logger = LoggerFactory.getLogger(ConfigController.class);

	private String modulePrefix = "/config/setup";	

	@Autowired(required = false)
	private PreferencesRegister preferencesRegister;
	
	@Autowired(required = false)
	private AbstractDriverBasedDataSource abstractDataSource;
	
	@Autowired(required = false)
	private AbstractRefreshableConfigApplicationContext appCtx;
	
	
	@RequestMapping(value = "/setup/startSetting.htm")
	public ModelAndView startSetting(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// --- not regist value ---

		ModelAndView mav = new ModelAndView();
		mav.setViewName(modulePrefix + "/start_setting"); // view name
        
		return mav;
	}
	
	@RequestMapping(value = "/setup/configpathRegist.json")
	public ModelAndView configpathRegist(String configpath , HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		
		// --- invoke and setting path ---
		SubmitResult submit = new SubmitResult();
		try {
			
			preferencesRegister.registConfigpath(configpath);
			submit.setSuccess(true);
			submit.setMsg("设置成功");			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			submit.setSuccess(false);
			submit.setMsg(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			
		}
		

		JsonConfig jcfg = new JsonConfig();	
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(submit, jcfg);		
		
		
		// --- reflesh ----

		
		ModelAndView mav = new ModelAndView();
		mav.addObject("json", jsonObject);
		mav.setViewName("json"); // view name
        
		
		return mav;
	}
	
	
	
	
	
}
